package com.shop2cn.iapi.grouponmgr.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.grouponmgr.parameter.GrouponCatalogListBean;
import com.shop2cn.iapi.grouponmgr.parameter.SaveGrouponReq;
import com.shop2cn.iapi.grouponmgr.service.GrouponCatalogListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnGrouponCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.YMTDateUtil;
/**
*拼团规格列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GrouponCatalogList{
	private static GrouponCatalogListBean grouponcataloglistBean;
	private static GrouponCatalogListCall grouponcataloglistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("拼团规格列表Test");
	}

	@Before
	public void caseUp() {
		grouponcataloglistBean=new GrouponCatalogListBean();
		grouponcataloglistCall=new GrouponCatalogListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(grouponcataloglistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_001() {
		Logger.start(true,"正确数据，查询拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
//			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
//			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,"yyyy-MM-dd HH:mm:ss"));
//			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd HH:mm:ss"));
//			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"),10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_002() {
		Logger.start(true,"正确数据，查询进行中的拼团");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_003() {
		Logger.start(true,"正确数据，查询未开始拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(2,"yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_004() {
		Logger.start(true,"正确数据，查询已结束拼团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"),3);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			Thread.sleep(5000);
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_005() {
		Logger.start(true,"没有输入活动状态，默认查询所有状态");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"),10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_006() {
		Logger.start(true,"没有输入商户编号");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,"yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			
//			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("400", grouponcataloglistCall.getString("code"), "验证code");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_007() {
		Logger.start(true,"拼团被删除，查询为空");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"),3);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			Thread.sleep(5000);
			
			Shop2cnGrouponCallService.DeleteGroupon(grouponProductId, mchId);
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_008() {
		Logger.start(true,"错误的团id，查询拼团");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3,"yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId+70);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("201", grouponcataloglistCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GrouponCatalogList_009() {
		Logger.start(true,"商品被下架，查询为空");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2,"yyyy-MM-dd HH:mm:ss"));
			String dateBeforeOrNextSecond = YMTDateUtil.getDateBeforeOrNextSecond(YMTDateUtil.getSysTime("yyyy-MM-dd HH:mm:ss"),10);
			savegrouponBean.setEndTime(dateBeforeOrNextSecond);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			Thread.sleep(2000);
			AddAgentProductCallService.SetOffshelf(pid, mchId);

			Thread.sleep(2000);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();
			
			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GrouponCatalogList_010() {
		Logger.start(true, "多规格变单规格，查询商品详情");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId, "AutoTest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId, pid);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});

			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid,
					catalogList.get(0), 100d);
			savegrouponBean.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-2, "yyyy-MM-dd HH:mm:ss"));
			savegrouponBean.setEndTime(YMTDateUtil.getBeforeOrNextDay(3, "yyyy-MM-dd HH:mm:ss"));
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);

			Thread.sleep(5000);
			
			ModifyAgentProductBean initModifyAgentProductBean = AddAgentProductCallService.initModifyAgentProductBean(pid, addAgentProductBean);
			ModifyCatalogDto modifyCatalogDto = initModifyAgentProductBean.getCatalogList().get(0);
			initModifyAgentProductBean.setCatalogList(Arrays.asList(modifyCatalogDto));
			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBean);
			Thread.sleep(5000);
			
			grouponcataloglistBean.setSellerId(mchId);
			grouponcataloglistBean.setProductId(pid);
			grouponcataloglistBean.setGrouponProductId(grouponProductId);
			grouponcataloglistCall.setData(grouponcataloglistBean);
			grouponcataloglistCall.callService();

			Logger.verifyEquals("200", grouponcataloglistCall.getString("code"), "验证code");
			JsonObject produ = grouponcataloglistCall.getJsonObject().getAsJsonArray("data").get(0).getAsJsonObject();
			Logger.verifyEquals(grouponProductId, produ.get("grouponProductId").getAsInt(), "判断活动id是否正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}